package com.jeesite.modules.rt.web;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.sys.entity.EmpUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.rt.entity.RtDevicePaoliang;
import com.jeesite.modules.rt.service.RtDevicePaoliangService;

/**
 * 跑量Controller
 * @author 李寒冰
 * @version 2024-10-15
 */
@Controller
@RequestMapping(value = "${adminPath}/rt/rtDevicePaoliang")
public class RtDevicePaoliangController extends BaseController {

	@Autowired
	private RtDevicePaoliangService rtDevicePaoliangService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public RtDevicePaoliang get(String id, boolean isNewRecord) {
		return rtDevicePaoliangService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("rt:rtDevicePaoliang:view")
	@RequestMapping(value = {"list", ""})
	public String list(RtDevicePaoliang rtDevicePaoliang, Model model) {
		model.addAttribute("rtDevicePaoliang", rtDevicePaoliang);
		return "modules/rt/rtDevicePaoliangList";
	}

	/**
	 * 导出跑量数据
	 */
	@RequiresPermissions("rt:rtDevicePaoliang:view")
	@RequestMapping(value = "exportData")
	public void exportData(RtDevicePaoliang rtDevicePaoliang, HttpServletResponse response) {
		Integer nightHeartbeatNum = rtDevicePaoliang.getNightHeartbeatNum();
		if(nightHeartbeatNum!=null ){
			rtDevicePaoliang.setNightHeartbeatNumSearch(rtDevicePaoliang.getSearchTypeWan(),nightHeartbeatNum);
			rtDevicePaoliang.setNightHeartbeatNum(null);
		}
		Integer heartbeatNum = rtDevicePaoliang.getHeartbeatNum();
		if(heartbeatNum!=null ){
			rtDevicePaoliang.setHeartbeatNumSearch(rtDevicePaoliang.getSearchType(),heartbeatNum);
			rtDevicePaoliang.setHeartbeatNum(null);
		}
		Double bandwidth = rtDevicePaoliang.getBandwidth();
		if(bandwidth!=null ){
			rtDevicePaoliang.setBandwidthSearch(rtDevicePaoliang.getSearchTypePaoliang(),bandwidth);
			rtDevicePaoliang.setBandwidth(null);
		}
		List<RtDevicePaoliang> list = rtDevicePaoliangService.findList(rtDevicePaoliang);
		Date workDate = rtDevicePaoliang.getWorkDate();
		String date = null;
		if(workDate==null){
			date = DateUtils.getDate("yyyyMMdd");
		}else{
			date = DateUtils.formatDate(workDate,"yyyyMMdd");
		}
		String fileName = "跑量数据" + date + ".xlsx";
		try(ExcelExport ee = new ExcelExport("跑量数据", RtDevicePaoliang.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}


	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("rt:rtDevicePaoliang:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<RtDevicePaoliang> listData(RtDevicePaoliang rtDevicePaoliang, HttpServletRequest request, HttpServletResponse response) {
		rtDevicePaoliang.setPage(new Page<>(request, response));
		Integer nightHeartbeatNum = rtDevicePaoliang.getNightHeartbeatNum();
		if(nightHeartbeatNum!=null ){
			rtDevicePaoliang.setNightHeartbeatNumSearch(rtDevicePaoliang.getSearchTypeWan(),nightHeartbeatNum);
			rtDevicePaoliang.setNightHeartbeatNum(null);
		}
		Integer heartbeatNum = rtDevicePaoliang.getHeartbeatNum();
		if(heartbeatNum!=null ){
			rtDevicePaoliang.setHeartbeatNumSearch(rtDevicePaoliang.getSearchType(),heartbeatNum);
			rtDevicePaoliang.setHeartbeatNum(null);
		}
		Double bandwidth = rtDevicePaoliang.getBandwidth();
		if(bandwidth!=null ){
			rtDevicePaoliang.setBandwidthSearch(rtDevicePaoliang.getSearchTypePaoliang(),bandwidth);
			rtDevicePaoliang.setBandwidth(null);
		}
		Page<RtDevicePaoliang> page = rtDevicePaoliangService.findPage(rtDevicePaoliang);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("rt:rtDevicePaoliang:view")
	@RequestMapping(value = "form")
	public String form(RtDevicePaoliang rtDevicePaoliang, Model model) {
		model.addAttribute("rtDevicePaoliang", rtDevicePaoliang);
		return "modules/rt/rtDevicePaoliangForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("rt:rtDevicePaoliang:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated RtDevicePaoliang rtDevicePaoliang) {
		rtDevicePaoliangService.save(rtDevicePaoliang);
		return renderResult(Global.TRUE, text("保存跑量成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("rt:rtDevicePaoliang:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(RtDevicePaoliang rtDevicePaoliang) {
		rtDevicePaoliangService.delete(rtDevicePaoliang);
		return renderResult(Global.TRUE, text("删除跑量成功！"));
	}
	
}